import java.util.Arrays;

public class test {
    public static void main(String[] args) {
//        PriorityQueue p1=new PriorityQueue();
//        int[]arr={2,};
//        p1.createHeap(arr);
//        p1.show();
//        p1.push(66);
//        int a=p1.poll();
//        System.out.println(a);
//        int top=p1.peek();
//        System.out.println(top);
        int[]arr={3,5,6,88,99,2,1,5,};
        System.out.println(Arrays.toString(arr));
    }

    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    private static void shiftDown(int[] array,int root,int len) {
        int parent = root;
        int child = (2*parent) + 1;
        while (child < len) {
            if(child+1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }

    private static void createHeap(int[] array) {
        for (int p = (array.length-1-1)/2; p >= 0 ; p--) {
            shiftDown(array,p,array.length);
        }
    }

    /**
     * 时间复杂度：O(n*logn)【不管数据是否有序 都是这样的】
     * 空间复杂度：O(1)
     * 稳定性：不稳定排序
     * @param array
     */
    public static void heapSort(int[] array) {
        createHeap(array);
        int end = array.length-1;
        while (end >= 0) {
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }


}
